import { ActionBase, ActionBaseOfCreep } from "../ActionBase";

 
/**
 * 放置site Action
 */
export class ActionDealBuy  extends ActionBase<IActionCMD<"dealbuy">>
{
    public execute(action: IActionCMD<"dealbuy">): ActionCode 
    {
        const room = action.arg.entity;
        const coreentity = room.getEntity("CoreEntity");
        const arg = action.arg.task;
        if(!coreentity.Terminal)
        {
            return 0;
        }
        const free_tnum =  coreentity.Terminal.store.getFreeCapacity();
        const sendnum = Math.min(free_tnum,arg.amount);
        if(coreentity.Terminal.cooldown  == 0)
        {
            const result = Game.market.deal(arg.orderId, arg.amount,room.name);
            if(result == OK)
            {
                // 发送成功
                arg.amount-=sendnum;
            }
            else if(result != ERR_FULL)
            {
                // 发送失败
                global.Log.Warning(`deal失败 code:${result} 删除任务   ${JSON.stringify(action.arg.task)}`);
                if(action.arg.task.taskid)
                {
                    room.deleteTaskById(action.arg.task.taskid,"terminal");
                }
                 
            }
        }
        return 0;
    }
}